package se.thoka.twt.client.utils.validation;

import se.thoka.twt.client.utils.StringUtils;

import com.google.gwt.user.client.ui.HasText;

/**
 * @author Thobias Karlsson, Thoka
 * @since 9 jan 2010
 */
public class MatchingContentValidation<T extends HasText> implements Validation {

	private String message;
	private T field;

	/**
	 * Creates a new instance of {@link MatchingContentValidation}
	 * @param field The form field to match with
	 */
	public MatchingContentValidation(T field){
		this(field, ValidationUtil.getStrings().contentMatchInvalid());
	}

	/**
	 * Creates a new instance of {@link MatchingContentValidation}.
	 * @param field The form field to match with
	 * @param message The error message
	 */
	public MatchingContentValidation(T field, String message){
		this.message = message;
		this.field = field;
	}

	/**
	 * @param field The form field to match with
	 */
	public void setMatchingContentField(T field){
		this.field = field;
	}
	
	@Override
	public String getDescription() {
		return message;
	}
	
	@Override
	public boolean validate(String value) {
		boolean result = false;
		if (!StringUtils.nullOrEmpty(value)){
			result = field.getText().equals(value); 
		}
		return result;

	}

}
